package org.activiti.designer.test;

import static org.junit.Assert.assertNotNull;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.test.ActivitiRule;
import org.junit.Test;

public class ProcessTestMyProcess {

	private String filename = "E:\\activiti-demo02\\src\\main\\resources\\diagrams\\HelloWorld.bpmn";

	ProcessEngine pe = ProcessEngines.getDefaultProcessEngine();// 默认的流程引擎自动加载classpath下的activiti.cfg.xml

	// @Rule
	public ActivitiRule activitiRule = new ActivitiRule();

	/**
	 * 部署流程
	 */
	@Test
	public void deployment() {
		Deployment deploy = pe.getRepositoryService().createDeployment().name("Hello world")
				.addClasspathResource("diagrams/HelloWorld.bpmn").deploy();
		System.out.println("部署id:" + deploy.getId());
		System.out.println("部署名称：" + deploy.getName());
	}

	public void startProcess() throws Exception {
		RepositoryService repositoryService = activitiRule.getRepositoryService();
		repositoryService.createDeployment().addInputStream("myProcess.bpmn20.xml", new FileInputStream(filename))
				.deploy();
		RuntimeService runtimeService = activitiRule.getRuntimeService();
		Map<String, Object> variableMap = new HashMap<String, Object>();
		variableMap.put("name", "Activiti");
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess", variableMap);
		assertNotNull(processInstance.getId());
		System.out.println("id " + processInstance.getId() + " " + processInstance.getProcessDefinitionId());
	}
}